package com.xqianli.bigdata.flink.transform;

import com.xqianli.bigdata.flink.utils.SensorReading;
import org.apache.flink.client.program.StreamContextEnvironment;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class TransformKeyBy {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamContextEnvironment.getExecutionEnvironment();
        // 从socket文本流读取数据
        DataStream<String> dataStream = env.socketTextStream("localhost", 7777);
        // KeyBy
        //        DataStream<SensorReading> mapStream = dataStream.map(new MapFunction<String, SensorReading>() {
        //            @Override
        //            public SensorReading map(String s) throws Exception {
        //                String[] split = s.split(",");
        //                return new SensorReading(split[0], new Long(split[1]), new Double(split[2]));
        //            }
        //        });

        DataStream<SensorReading> mapStream = dataStream.map(item -> {
            String[] split = item.split(",");
            return new SensorReading(split[0], new Long(split[1]), new Double(split[2]));
        });

        KeyedStream<SensorReading, String> keyedStream = mapStream.keyBy(SensorReading::getId);

        keyedStream.print();



        env.execute();
    }
}
